/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gpstracking.DA;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.gpstracking.BE.Area;
import com.gpstracking.BE.Usuario;

/**
 *
 * @author jmj
 */
public class UsuarioDA {

    

    public static boolean save(Usuario instance) {
    	
    	Session hbsession = null;
        try{
        	hbsession = HUtil.getSession();
            Transaction tx = hbsession.beginTransaction();
            tx.begin();
        	
            hbsession.save(instance);
            
            
            
            tx.commit();
            
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            
            return false;
        }
    }

    public static Usuario get(int id) {
    	Session hbsession = HUtil.getSession();
    	Transaction tx = hbsession.beginTransaction();
		tx.begin();
        Query query = hbsession.createQuery("FROM Usuario WHERE idUsuario = " + id);
        List<Usuario> res = (List<Usuario>) query.list();
        
        return res.isEmpty() ? null : res.get(0);
    }
    
    public static Usuario check(String user, String pass) {
    	Session hbsession = HUtil.getSession();
    	Transaction tx = hbsession.beginTransaction();
		tx.begin();
        Query query = hbsession.createQuery("FROM Usuario WHERE usuario = :user AND clave = :pass");
        query.setParameter("user", user);
        query.setParameter("pass", pass);
        List<Usuario> res = (List<Usuario>) query.list();

        return res.isEmpty() ? null : res.get(0);
    }
}

